import { User } from "../screens/project-list/search-panel";
import { createSlice } from "@reduxjs/toolkit";
import * as auth from "auth-provider";
import { AuthForm, bootstrapUser } from "../context/auth-context";
import { AppDispatch, RootState } from "./index";

interface State {
  user: User | null
}

const initialState: State = {
  user: null
};

export const authSlice = createSlice({
  name: "auth",
  initialState,
  reducers: {
    setUser (state, action) {
      state.user = action.payload;
    }
  }
});

const { setUser } = authSlice.actions;

export const selectUser = (state: RootState) => state.auth.user

export const login = (form: AuthForm) => (dispatch: AppDispatch) => {
  return auth.login(form).then(user => dispatch(setUser(user)));
};

export const register = (form: AuthForm) => (dispatch: AppDispatch) => {
  return auth.register(form).then(user => dispatch(setUser(user)));
};

export const logout = () => (dispatch: AppDispatch) => auth.logout().then(() => dispatch(setUser(null)));

export const bootstrap = () => (dispatch: AppDispatch) => bootstrapUser().then(user => dispatch(setUser(user)));
